package cn.dansj.bean;

import cn.dansj.common.utils.enums.GlobalEnv;
import cn.dansj.common.utils.enums.ResponseCode;
import cn.dansj.common.utils.transfer.DateTime;
import cn.dansj.common.utils.transfer.DefaultMethods;
import cn.dansj.common.utils.transfer.StringUtils;
import cn.dansj.common.utils.transfer.Transformation;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class InterfaceResponse extends DefaultMethods implements Serializable {
    @Serial
    private static final long serialVersionUID = GlobalEnv.SerialVersionUID;
    private int curPage;
    private int totalPage;
    private int pageSize;
    private long curRecords;
    private long totalRecords;
    private Object data;
    private final String responseStartTime;
    private String responseEndTime;
    private String interfaceName;
    private boolean status;
    private String message;
    private String version;
    private int responseCode = 200;
    private final List<String> sourceCode = new ArrayList<>();

    public InterfaceResponse(int curPage, int pageSize, String interfaceName) {
        this.curPage = curPage;
        this.pageSize = pageSize;
        this.responseStartTime = DateTime.getInstance().stringTimestamp();
        this.interfaceName = interfaceName;
    }

    public InterfaceResponse() {
        this.responseStartTime = DateTime.getInstance().stringTimestamp();
    }

    public InterfaceResponse(String interfaceName) {
        this.responseStartTime = DateTime.getInstance().stringTimestamp();
        this.interfaceName = interfaceName;
    }

    public InterfaceResponse setCurPage(int curPage) {
        this.curPage = curPage;
        return this;
    }

    public InterfaceResponse setPageSize(int pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    public InterfaceResponse setTotalPage(int totalPage) {
        this.totalPage = totalPage;
        return this;
    }

    public InterfaceResponse setCurRecords(long curRecords) {
        this.curRecords = curRecords;
        return this;
    }

    public InterfaceResponse setTotalRecords(long totalRecords) {
        this.totalRecords = totalRecords;
        return this;
    }

    public InterfaceResponse setData(Object data) {
        this.data = data;
        return this;
    }

    public InterfaceResponse setStatus(boolean status) {
        this.status = status;
        return this;
    }

    public InterfaceResponse setMessage(String message) {
        this.message = message;
        return this;
    }

    public InterfaceResponse setByInterfaceResponseCode(ResponseCode responseCode) {
        this.message = responseCode.getError();
        this.responseCode = responseCode.getStatusCode();
        return this;
    }

    public InterfaceResponse setResponseEndTime(String responseEndTime) {
        this.responseEndTime = responseEndTime;
        return this;
    }

    public InterfaceResponse setResponseEndTime() {
        this.responseEndTime = DateTime.getInstance().stringTimestamp();
        return this;
    }

    public int getCurPage() {
        return curPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public long getCurRecords() {
        return curRecords;
    }

    public long getTotalRecords() {
        return totalRecords;
    }

    public Object getData() {
        return data;
    }

    public String getInterfaceName() {
        return interfaceName;
    }

    public int getPageSize() {
        return pageSize;
    }

    public boolean getStatus() {
        return status;
    }

    public String getMessage() {
        return message;
    }

    public String getResponseStartTime() {
        return responseStartTime;
    }

    public String getResponseEndTime() {
        return responseEndTime;
    }

    public String getVersion() {
        return version;
    }

    public InterfaceResponse setVersion(String version) {
        this.version = version;
        return this;
    }

    public int getResponseCode() {
        return responseCode;
    }

    public InterfaceResponse setResponseCode(int responseCode) {
        this.responseCode = responseCode;
        return this;
    }

    public InterfaceResponse setInterfaceName(String interfaceName) {
        this.interfaceName = interfaceName;
        return this;
    }

    public String toJsonString() {
        return Transformation.toJSONString(this, true);
    }

    public String getSourceCode() {
        return StringUtils.join(sourceCode, ";\n\n");
    }

    public InterfaceResponse setSourceCode(String sourcecode) {
        this.sourceCode.add(sourcecode);
        return this;
    }

    public InterfaceResponse resetSourceCode(String sourcecode) {
        this.sourceCode.clear();
        this.sourceCode.add(sourcecode);
        return this;
    }
}
